package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * ClassName: CategoryController
 * Package: com.sky.controller.admin
 * Description:分类的相关接口
 *
 * @Author: Zhang Peiyuan
 * @Create: 2024-12-18-10:56
 * @Version: v1.0
 */
@RestController
@RequestMapping("/admin/category")
@Slf4j
public class CategoryController {
    @Resource
    public CategoryService categoryService;

    /**
     * 新增分类
     *
     * @param categoryDTO
     * @return
     */
    @PostMapping()
    public Result addCategory(@RequestBody CategoryDTO categoryDTO) {
        categoryService.addCategory(categoryDTO);
        return Result.success();
    }

    /**
     * 分页查询分类
     * @param type
     * @param name
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> queryCategoryPage(Integer type, String name,
                                                @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                @RequestParam(value = "size", defaultValue = "10") Integer size) {
        return Result.success(categoryService.queryCategoryPage(type, name, page, size));
    }

    /**
     * 更新分类状态
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    public Result updateCategoryStatus(@PathVariable Integer status, @RequestParam(value="id") Long id) {
        categoryService.updateCategoryStatus(status, id);
        return Result.success();
    }

    /**
     * 更新分类
     * @param categoryDTO
     * @return
     */
    @PutMapping()
    public Result updateCategory(@RequestBody CategoryDTO categoryDTO) {
        categoryService.updateCategory(categoryDTO);
        return Result.success();
    }

    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    @GetMapping("/list")
    public Result<List<Category>> listCategory(@RequestParam(value="type") Integer type) {
        return Result.success(categoryService.listCategory(type));
    }

    @DeleteMapping()
    public Result deleteCategory(@RequestParam(value="id") Long id) {
        categoryService.deleteCategory(id);
        return Result.success();
    }
}
